package controls;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.Timer;

import model.TaskElement;

public class CenterTimer {
	private final int _delay = 1000;
	private ArrayList<Refreshable> _controls = new ArrayList<Refreshable>();
	private LinkedList<Refreshable> _leftTaskElements = new LinkedList<Refreshable>();
	private LinkedList<Refreshable> _rightTaskElements = new LinkedList<Refreshable>();
	
	public void addToControlRefreshList(Refreshable element) {
		//synchronized(_controls){
			_controls.add(element);
		//}
	}
	
	public void removeFromControlRefreshList(Refreshable element){			
		//synchronized(_controls){
			_controls.remove(element);
		//}
	}
	
	public void addToLeft(Refreshable element){
		//synchronized(_leftTaskElements){
			_leftTaskElements.add(element);
		//}
	}	
	
	public void removeFromLeft(Refreshable element){
		//synchronized(_leftTaskElements){
			_leftTaskElements.remove(element);
		//}
	}
	
	public void addToRight(Refreshable element){
		//synchronized(_rightTaskElements){
			_rightTaskElements.add(element);
		//}
	}
	
	public void removeFromRight(Refreshable element){
		//synchronized(_rightTaskElements){
			_rightTaskElements.remove(element);
		//}
	}

	private CenterTimer() {
		_timer = new Timer(_delay, timerListener);
	}

	private static CenterTimer instance;

	public static CenterTimer getInstance() {
		if (instance == null)
			instance = new CenterTimer();
		return instance;
	}

	private Timer _timer;
	private ActionListener timerListener = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			
			Iterator<Refreshable> itrLeft = _leftTaskElements.listIterator(0);
			while(itrLeft.hasNext())
				itrLeft.next().refresh();
			
			Iterator<Refreshable> itrRight = _rightTaskElements.listIterator(0);
			while(itrRight.hasNext())
				itrRight.next().refresh();
			
			for(int i = _controls.size() - 1; i >= 0; i--){
				_controls.get(i).refresh();
			}
		}
	};

	public void startTimer() {
		_timer.setRepeats(true);
		_timer.start();
	}

	public void stopTimer() {
		_timer.stop();
	}
	
	public ArrayList<TaskElement> getLeftWaitingTaskElements(){
		ArrayList<TaskElement> temp = new ArrayList<TaskElement>();		
	
		Iterator<Refreshable> itr = _leftTaskElements.listIterator(0);
		while(itr.hasNext())
			temp.add((TaskElement)itr.next());
		
		temp.trimToSize();
		
		if(temp.size() == 0)
			return null;
		else			
			return temp;
	}
	
	public ArrayList<TaskElement> getRightWaitingTaskElements(){
		ArrayList<TaskElement> temp = new ArrayList<TaskElement>();		
	
		Iterator<Refreshable> itr = _rightTaskElements.listIterator(0);
		while(itr.hasNext())
			temp.add((TaskElement)itr.next());
		
		temp.trimToSize();
		
		if(temp.size() == 0)
			return null;
		else			
			return temp;
	}
}
